import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpClient {

    private int port;
    private String ip;
    DatagramSocket socket;


    public UdpClient(int pot,String ip) throws SocketException {
        socket=new DatagramSocket();

        this.port=pot;
        this.ip=ip;

    }



    private void start() throws IOException {

        System.out.println("客户端启动");
        Scanner scanner=new Scanner(System.in);

        while(true){

            System.out.println("请输入想要发送的内容");


            if(!scanner.hasNext()){
                break;
            }


            String request=scanner.next();


            DatagramPacket requestpacket=new DatagramPacket(
                    request.getBytes(),0,request.getBytes().length,InetAddress.getByName(ip),port);

            socket.send(requestpacket);

            DatagramPacket respon=new DatagramPacket(new byte[4096],4096);

            socket.receive(respon);

            String respon1=new String(respon.getData(),0,respon.getLength());

            System.out.println(respon1);


        }


    }

    public static void main(String[] args) throws IOException {
        UdpClient client=new UdpClient(9090,"127.0.0.1");
        client.start();
    }


}
